package com.hbx.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.commonutils.R;
import com.hbx.eduservice.entity.EduTeacher;
import com.hbx.eduservice.entity.dto.TeacherQuery;
import com.hbx.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2023-09-24
 */
@Api(tags = {"讲师管理"})
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/findAllTeacher")
    @ApiOperation("查询所有讲师")
    public R findTeacher(){
        return R.ok().data("items",eduTeacherService.list(null));
    }

    /**
     * 逻辑删除讲师
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("逻辑删除讲师")
    public R removeTeacher(@PathVariable String id){
        if(eduTeacherService.removeById(id))
            return R.ok();
        else
            return R.error();
    }

    /**
     * 分页查询讲师
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageList(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(current, limit);
        eduTeacherService.page(pageParam,null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    /**
     * 条件分页查询 讲师
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "条件分页查询讲师")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageInfo = new Page<>(current,limit);
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(!StringUtils.isEmpty(teacherQuery.getName()),EduTeacher::getName,teacherQuery.getName());
        queryWrapper.eq(!StringUtils.isEmpty(teacherQuery.getLevel()),EduTeacher::getLevel,teacherQuery.getLevel());
        queryWrapper.ge(!StringUtils.isEmpty(teacherQuery.getBegin()),EduTeacher::getGmtCreate,teacherQuery.getBegin());
        queryWrapper.le(!StringUtils.isEmpty(teacherQuery.getEnd()),EduTeacher::getGmtCreate,teacherQuery.getEnd());

        queryWrapper.orderByDesc(EduTeacher::getGmtCreate);

        eduTeacherService.page(pageInfo,queryWrapper);
        List<EduTeacher> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean flag = eduTeacherService.save(teacher);
        if(flag){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean flag = eduTeacherService.updateById(teacher);
        if(flag){
            return R.ok();
        }
        return R.error();
    }

}

